﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame4
{
    class EventManager
    {
        static private EventManager eventManager = null;
        private Dictionary<String,AIObject> aiObjects;

        private EventManager()
        {
            aiObjects = new Dictionary<String,AIObject>();
        }

        static public EventManager getInstance()
        {
            if (eventManager == null)
                eventManager = new EventManager();
            return eventManager;
        }
        public void reset()
        {
            aiObjects = new Dictionary<String, AIObject>();
        }
        public void update()
        {
            Boolean error = false;
            do
            {
                error = false;
                int numErrors = 0;
                AIObject aiObject;
                try
                {
                    foreach (KeyValuePair<String, AIObject> obj in aiObjects)
                    {
                        aiObject = obj.Value;
                        try
                        {
                            //System.Console.WriteLine("updating: " + aiObject.Id);
                            if (aiObject != null)
                            {
                                //System.Console.WriteLine(aiObject.Id);
                                aiObject.update();
                            }
                        }
                        catch (Exception e)
                        {
                            //numErrors++;
                            //if (numErrors == 10)
                            //{
                           System.Console.WriteLine(e.ToString());
                                System.Console.WriteLine("ha cascado: " + aiObject.Id + " " + aiObject.IdEmitter + aiObject.Type);
                                System.Console.ReadKey();
                                //if (aiObject != null)
                                //    GameInfo.getInstance().removeObject(aiObject.Id);
                            //}
                            //System.Console.ReadKey();
                            error = true;
                        }
                    }
                }
                catch (Exception e)
                {

                }

            } while (error == true);
        }

        public AIObject getAIObject(String id)
        {
            return aiObjects[id];
        }

        public void deleteAIObject(String id)
        {
            aiObjects.Remove(id);
        }

        public void registerAIObject(AIObject aiObject)
        {
            aiObjects.Add(aiObject.Id,aiObject);
        }
    }
}
